#include "token.h"

void Encode_Jwt(char* issuer,char* subject,char* audience,char* key,char* jwt_str)
{
    char* jwt;
    size_t jwt_length;
    struct l8w8jwt_encoding_params params;
    l8w8jwt_encoding_params_init(&params);

    params.alg = L8W8JWT_ALG_HS256;

    params.sub = subject;
    params.iss = issuer;
    params.aud = audience;

    params.iat = time(NULL);
    params.exp = time(NULL) + 60;

    params.secret_key = (unsigned char*)key;
    params.secret_key_length = strlen(params.secret_key);

    params.out = &jwt;
    params.out_length = &jwt_length;

    int r = l8w8jwt_encode(&params);
    if(r == L8W8JWT_SUCCESS)
    {
        strcpy(jwt_str,jwt);
    }

    l8w8jwt_free(jwt);
}

void SendToken(int peerfd,int port,int usrid)
{
    token_t Node;
    memset(&Node,0,sizeof(Node));
    Encode_Jwt("server","client","client","luyimin",Node.Token);
    Node.port = port;
    Node.userid = usrid;
    printf("发送Token开始\n");
    sendMsg(peerfd,(char*)&Node);
    printf("发送Token完毕\n");
}